<?php

namespace ms\modules\article\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use ms\modules\article\models\ArticleInfo;

/**
 * SearchArticleInfo represents the model behind the search form about `ms\modules\article\models\ArticleInfo`.
 */
class SearchArticleInfo extends ArticleInfo
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'img_id', 'click_num', 'nav_id', 'status', 'icon', 'publish_time'], 'integer'],
            [['title', 'content', 'abstract', 'author','type'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ArticleInfo::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'img_id' => $this->img_id,
            'click_num' => $this->click_num,
            'nav_id' => $this->nav_id,
            'status' => $this->status,
            'icon' => $this->icon,
            'publish_time' => $this->publish_time,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'abstract', $this->abstract])
            ->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'author', $this->author]);

        return $dataProvider;
    }
}
